package cn.livingCloud.controller.core;

import cn.livingCloud.dto.*;
import cn.livingCloud.entity.core.CaseStatus;
import cn.livingCloud.service.CaseService;
import cn.livingCloud.utils.BindingResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 案件控制器
 * Created by DingJie on 2016/6/2.
 */
@Controller
@RequestMapping("/admin/cmc/case")
public class CaseController {
    @Autowired
    private CaseService service;

//    //案件列表
//    @RequestMapping("/list.htm")
//    public String loadList(CaseListDto listDto, Model model) {
//        service.loadCaseList(listDto);
//        model.addAttribute("listDto", listDto);
//        return "cmc/case/list";
//    }

    //未完成案件列表
    @RequestMapping("/un_complete_list.htm")
    public String unCompleteList(CaseListDto listDto, Model model) {
        listDto.setListStatus(CaseListStatus.UN_COMPLETE);
        if (listDto.getStatus() == null) {
            listDto.setStatus(CaseStatus.NO_HANDLE);
        }
        service.loadCaseList(listDto);
        model.addAttribute("listDto", listDto);
        return "cmc/case/list";
    }

    //待审核案件列表
    @RequestMapping("/un_pending_list.htm")
    public String pendingList(CaseListDto listDto, Model model) {
        listDto.setListStatus(CaseListStatus.PENDING);
        if (listDto.getStatus() == null) {
            listDto.setStatus(CaseStatus.PENDING);
        }
        service.loadCaseList(listDto);
        model.addAttribute("listDto", listDto);
        return "cmc/case/list";
    }

    //锁定案件列表
    @RequestMapping("/lock_list.htm")
    public String lockList(CaseListDto listDto, Model model) {
        listDto.setListStatus(CaseListStatus.LOCK);
        if (listDto.getStatus() == null) {
            listDto.setStatus(CaseStatus.LOCK);
        }
        service.loadCaseList(listDto);
        model.addAttribute("listDto", listDto);
        return "cmc/case/list";
    }

    //已完成案件列表
    @RequestMapping("/complete_list.htm")
    public String completeList(CaseListDto listDto, Model model) {
        listDto.setListStatus(CaseListStatus.COMPLETE);
        listDto.setMulti(false);
        if (listDto.getStatus() == null) {
            listDto.setStatus(CaseStatus.FINISH);
        }
        service.loadCaseList(listDto);
        model.addAttribute("listDto", listDto);
        return "cmc/case/list";
    }

    //顽固案件
    @RequestMapping("/stubborn_list.htm")
    public String stubbornList(CaseListDto listDto, Model model) {
        listDto.setListStatus(CaseListStatus.STUBBORN);
        if (listDto.getStatus() == null) {
            listDto.setStatus(CaseStatus.ASSIGNMENT);
        }
        if (listDto.getStubbornCount() < 0) {
            listDto.setStubbornCount(3);
        }
        service.loadCaseList(listDto);
        model.addAttribute("listDto", listDto);
        return "cmc/case/list";
    }

    //案件添加
    @RequestMapping("/edit.htm")
    public String edit(String uuid, String creatorUuid, Model model) {
        CaseFormDto formDto = service.loadCaseFromDto(uuid, creatorUuid);
        model.addAttribute("formDto", formDto);
        return "cmc/case/edit";
    }

    //案件添加
    @RequestMapping(value = "/edit.htm", method = RequestMethod.POST)
    public String edit(@ModelAttribute("formDto") @Valid CaseFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "cmc/case/edit";
        }
        service.edit(formDto);
        model.addAttribute("edit", "success");
        return "redirect:un_complete_list.htm";
    }

    //案件详情
    @RequestMapping("/info.htm")
    public String info(String uuid, Model model) {
        CaseInfoDto info = service.loadCaseInfo(uuid);
        model.addAttribute("info", info);
        return "cmc/case/info";
    }

    //案件图片列表
    @RequestMapping("/images.htm")
    public String imageList(@RequestParam("uuid") String uuid, boolean result, Model model) {
        CaseImageListDto imageListDto = service.loadImageList(uuid, result);
        model.addAttribute("images", imageListDto);
        return "cmc/case/images";
    }

    //案件档案
    @RequestMapping("/records.htm")
    public String records(@RequestParam("uuid") String uuid, Model model) {
        CaseRecordsListDto listDto = service.loadRecords(uuid);
        model.addAttribute("listDto", listDto);
        return "cmc/case/records";
    }

    //修改案件类别
    @RequestMapping("/change_category")
    public String changeCategoryForm(@RequestParam("uuid") String uuid, Model model) {
        CaseCategoryChangeFormDto formDto = service.loadChangeCategoryForm(uuid);
        model.addAttribute("formDto", formDto);
        return "cmc/case/change_category";
    }

    //修改案件类别
    @RequestMapping("/change_category_submit")
    @ResponseBody
    public JsonDto changeCategorySubmit(@ModelAttribute("formDto") @Valid CaseCategoryChangeFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.changeCategory(formDto);
    }

    //上报案件
    @RequestMapping("/report")
    public String reportCaseForm(@RequestParam("uuid") String uuid, Model model) {
        CaseReportFormDto formDto = service.loadReportForm(uuid);
        model.addAttribute("formDto", formDto);
        return "cmc/case/report";
    }

    //上报案件
    @RequestMapping(value = "/report", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto reportCaseSubmit(@Valid CaseReportFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.reportCase(formDto);
    }

    //作废案件
    @RequestMapping("/cancel")
    public String cancelForm(@RequestParam("uuid") String uuid, Model model) {
        CaseCancelFormDto formDto = new CaseCancelFormDto(uuid);
        model.addAttribute("formDto", formDto);
        return "cmc/case/cancel";
    }

    //作废案件
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto cancelSubmit(@Valid CaseCancelFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.cancelCase(formDto);
    }

    //位置描述 查看内容
    @RequestMapping("/content.htm")
    public String showContent(@RequestParam("uuid") String uuid, Model model) {
        CaseDto caseDto = service.loadContent(uuid);
        model.addAttribute("caseDto", caseDto);
        return "cmc/case/content";
    }

    //完成案件
    @RequestMapping("/finish")
    public String finishForm(@RequestParam("uuid") String uuid, Model model) {
        CaseFinishFormDto formDto = new CaseFinishFormDto(uuid);
        model.addAttribute("formDto", formDto);
        return "cmc/case/finish";
    }

    //完成案件
    @RequestMapping(value = "/finish", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto finishForm(@Valid CaseFinishFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.finishCase(formDto);
    }

    //分派案件
    @RequestMapping("/assignment")
    public String assignmentForm(@RequestParam("caseUuid") String caseUuid, String deptUuid, Model model) {
        CaseAssignmentFormDto formDto = service.loadAssignmentForm(caseUuid, deptUuid);
        model.addAttribute("formDto", formDto);
        return "cmc/case/assignment";
    }

    //分派案件
    @RequestMapping(value = "/assignment", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto assignmentSubmit(@Valid CaseAssignmentFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.assignmentCase(formDto);
    }

    //审核案件
    @RequestMapping("/pending")
    public String pending(@RequestParam("uuid") String uuid, Model model) {
        CasePendingFormDto formDto = service.loadPendingForm(uuid);
        model.addAttribute("formDto", formDto);
        return "cmc/case/pending";
    }

    //审核案件
    @RequestMapping(value = "/pending", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto pending(@Valid CasePendingFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.pendingCase(formDto);
    }

    //案件概要
    @RequestMapping(value = "/profile")
    @ResponseBody
    public CaseProfileDto getProfile() {
        return service.loadCaseProfile();
    }

}
